﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms; 
using Einstein.Data;
using Einstein.Utility;
using DevComponents.DotNetBar;
using Einstein.Group.DAL;

namespace Einstein.Platform.Window.Config
{
    public partial class Config_Operator : Office2007Form
    {
        public Config_Operator()
        {
            InitializeComponent();

            BindOperator();
        }

        void BindOperator()
        {
            OperatorDAL dal = new OperatorDAL();
            dgList.PrimaryGrid.DataSource = dal.SelectAll();
            Config_Operator_Edit.Enabled = false;
            Config_Operator_Delete.Enabled = false;
        }

        Operator currentData;
        public Operator CurrentData
        {
            get
            {
                return currentData;
            }
            set
            {
                Config_Operator_Edit.Enabled = true;
                Config_Operator_Delete.Enabled = true;
                currentData = value;
            }
        }
        private void Config_Operator_New_Click(object sender, EventArgs e)
        {
            Config_Operator_New form = new Config.Config_Operator_New();
            form.Text = "新建操作用户";
            if (form.ShowDialog() == DialogResult.OK)
            {
                BindOperator();
            }
        }

        private void Config_Operator_Edit_Click(object sender, EventArgs e)
        {
            if (CurrentData != null)
            { 
                Config_Operator_New form = new Config_Operator_New();
                form.Text = "编辑操作用户";
                form.BindInfo = CurrentData;
                form.BindData();
                if (form.ShowDialog() == DialogResult.OK)
                {
                    BindOperator();
                }
            }
            else
            {
                BoxManager.ShowWarning("您没有选择任何行，请重试。");
            }
        }

        private void Config_Operator_Delete_Click(object sender, EventArgs e)
        {
            if (CurrentData != null)
            {
                if (CurrentData.IsSys && CurrentData.IsSys)
                {
                    BoxManager.ShowWarning("系统默认用户不能删除！");
                    return;
                }

                if (BoxManager.ShowDeleteAsk() != DialogResult.OK)
                {
                    return;
                }

                OperatorDAL dal = new OperatorDAL();
                bool result = dal.Delete(new Operator { UserID = CurrentData.UserID });
                if (result)
                {
                    BindOperator();
                    BoxManager.ShowDeleteSuccess();
                }
                else
                {
                    BoxManager.ShowDeleteFail();
                }
            }
            else
            {
                BoxManager.ShowWarning("您没有选择任何行，请重试。");
            }
        }

        private void dgList_RowClick(object sender, DevComponents.DotNetBar.SuperGrid.GridRowClickEventArgs e)
        {
            Operator currentItem = ((DevComponents.DotNetBar.SuperGrid.GridRow)e.GridRow).DataItem as Operator;
            if (currentItem != null)
            {
                CurrentData = currentItem;
            }
        }
    }
}
